package service

import (
	"ruoyi-go/app/admin/model/system"
	"ruoyi-go/app/admin/model/tools"
	"ruoyi-go/pkg/mysql"
)

var SysDeptService = &sysDeptService{}

type sysDeptService struct{}

// GetDeptList 分页查询
func (service sysDeptService) GetDeptList(params tools.SearchTableDataParam, isPaging bool) ([]system.SysDeptResult, int64) {
	var total int64
	var pageNum = params.PageNum
	var pageSize = params.PageSize
	offset := (pageNum - 1) * pageSize
	dept := params.Other.(system.SysDept)

	var list []system.SysDeptResult
	var db = mysql.MysqlDb().Model(&[]system.SysDept{})

	var deptId = dept.DeptId
	if deptId != 0 {
		db.Where("dept_id = ?", deptId)
	}

	var parentId = dept.ParentId
	if parentId != 0 {
		db.Where("parent_id = ?", parentId)
	}

	var deptName = dept.DeptName
	if deptName != "" {
		db.Where("dept_name like concat('%', ?, '%')", deptName)
	}

	var status = dept.Status
	if status != "" {
		db.Where("status = ?", status)
	}
	db.Order("parent_id, order_num")

	if err := db.Count(&total).Error; err != nil {
		return nil, 0
	}
	if isPaging {
		if err := db.Offset(offset).Limit(pageSize).Find(&list).Error; err != nil {
			return nil, 0
		}
	} else {
		if err := db.Find(&list).Error; err != nil {
			return nil, 0
		}
	}

	return list, total
}

func (service sysDeptService) GetDeptInfo(deptId string) system.SysDept {
	var dept system.SysDept
	err := mysql.MysqlDb().Where("dept_id = ?", deptId).First(&dept).Error
	if err != nil {
		panic(err.Error())
	}
	return dept
}

func (service sysDeptService) SaveDept(sysDept system.SysDept) {
	sysDept.DelFlag = "0"
	err := mysql.MysqlDb().Model(&sysDept).Create(&sysDept).Error
	if err != nil {
		panic(err.Error())
	}
}

func (service sysDeptService) UpDataDept(sysDept system.SysDept) {
	err := mysql.MysqlDb().Updates(&sysDept).Error
	if err != nil {
		panic(err.Error())
	}
}

/*单个删除*/
func (service sysDeptService) DeleteDept(deptId string) {
	//if (deptService.hasChildByDeptId(deptId))
	//        {
	//            return AjaxResult.error("存在下级部门,不允许删除");
	//        }
	//        if (deptService.checkDeptExistUser(deptId))
	//        {
	//            return AjaxResult.error("部门存在用户,不允许删除");
	//        }
	//是否有数据权限
	err := mysql.MysqlDb().Where("dept_id = ?", deptId).Delete(&system.SysDept{}).Error
	if err != nil {
		panic(err.Error())
	}
}

type TreeSelect struct {
	Id       int          `json:"id"`
	Label    string       `json:"label"`
	Children []TreeSelect `json:"children"`
}

func (service sysDeptService) SelectDeptTreeList() []TreeSelect {
	var deptResults []TreeSelect
	var depts []system.SysDept
	err := mysql.MysqlDb().Where("del_flag = '0'").Order("parent_id, order_num").Find(&depts).Error
	if err != nil {
		panic(err.Error())
	}

	for i := 0; i < len(depts); i++ {
		dept := depts[i]
		deptId := dept.DeptId
		name := dept.DeptName
		pId := dept.ParentId
		if pId == 0 {
			tChild := service.getChildList(depts, deptId)
			treeSelect := TreeSelect{
				Id:       deptId,
				Label:    name,
				Children: tChild,
			}
			deptResults = append(deptResults, treeSelect)
		}
	}

	return deptResults
}

func (service sysDeptService) getChildList(depts []system.SysDept, deptId int) []TreeSelect {
	var tlist []TreeSelect
	for i := 0; i < len(depts); i++ {
		dept1 := depts[i]
		id := dept1.DeptId
		pId := dept1.ParentId
		name := dept1.DeptName

		if pId == deptId {
			tChild := service.getChildList(depts, id)
			tree := TreeSelect{
				Id:       id,
				Label:    name,
				Children: tChild,
			}
			tlist = append(tlist, tree)
		}

	}
	return tlist
}
